
from selenium import webdriver
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Chrome(object):

    def __init__(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"')
        proxy_socks_argument = '--proxy-server=socks5://127.0.0.1:1080'

        chrome_options.add_argument(proxy_socks_argument)
        chrome_options.add_argument("-lang=zh-cn")
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument("--disable-software-rasterizer")
        self.driver = webdriver.Chrome(options=chrome_options)
        self.driver.set_window_size(1280,800)

    def get(self,url):
        self.driver.get(url)
        time.sleep(5)
        html = self.driver.page_source
        return html

    def get_twitter(self,url):
        try:
            self.driver.get(url)
            WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH,'//div[@class="css-901oao r-18jsvk2 r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-bnwqim r-qvutc0"]')))
            return  self.driver.page_source
        except Exception:
            return None

    def quit(self):
        print('程序退出')
        self.driver.quit()

chrome = Chrome()